<script setup lang="ts">
import { patientMockConsignment } from '@/api/order'
import { ref } from 'vue'
import { useRoute } from 'vue-router'
import type { patientMockConsignmentApi } from '@/types/order'
import AMapLoader from '@amap/amap-jsapi-loader'
import { onMounted } from 'vue'
import startIcon from '@/assets/start.png'
import endIcon from '@/assets/end.png'
import currIcon from '@/assets/car.png'
const route = useRoute()
const info = ref<patientMockConsignmentApi>()
const getData = async () => {
  const res = await patientMockConsignment(route.params.id as string)
  info.value = res.data
  console.log('物流数据', res)
}

// 地图相关
window._AMapSecurityConfig = {
  securityJsCode: '99240abf1188823cd88cfe635a521af3'
}
const initMap = () => {
  const centerPoint = [
    info.value?.currentLocationInfo.longitude,
    info.value?.currentLocationInfo.latitude
  ]
  AMapLoader.load({
    key: 'b5a3290bb8e23f2361bcc36be9435bcd', // 申请好的Web端开发者Key，首次调用 load 时必填
    version: '2.0', // 指定要加载的 JSAPI 的版本，缺省时默认为 1.4.15
    plugins: [''] // 需要使用的的插件列表，如比例尺'AMap.Scale'等
  })
    .then(AMap => {
      const map = new AMap.Map('map', {
        //设置地图容器id
        viewMode: '3D', //是否为3D地图模式
        zoom: 12, //初始化地图级别
        mapStyle: 'amap://styles/normal',
        pitch: 30,
        center: centerPoint //初始化地图中心点位置  [经度,纬度]
      })
      // 路线规划
      var startLngLat = [
        info.value?.logisticsInfo[0].longitude,
        info.value?.logisticsInfo[0].latitude
      ]
      const len = info.value?.logisticsInfo.length! - 1
      var endLngLat = [
        info.value?.logisticsInfo[len].longitude,
        info.value?.logisticsInfo[len].latitude
      ]
      AMap.plugin('AMap.Driving', function () {
        var driving = new AMap.Driving({
          map: map,
          // 驾车路线规划策略，AMap.DrivingPolicy.LEAST_TIME是最快捷模式
          policy: AMap.DrivingPolicy.LEAST_TIME,
          showTraffic: false,
          hideMarkers: true
        })

        const otherPoint = {
          waypoints: info.value?.logisticsInfo
            .slice(1, -1)
            .map(item => [item.longitude, item.latitude])
        }

        driving.search(startLngLat, endLngLat, otherPoint, function (status: string, result: any) {
          // 未出错时，result即是对应的路线规划方案
          if (status === 'complete') {
            // 过渡动画
            setTimeout(() => {
              map.setCenter(centerPoint)
              map.setZoom(12)
            }, 3000)

            console.log('其它处理')
          }
        })
      })

      // 标记点处理
      // 起点   import
      const startMarker = new AMap.Marker({
        position: startLngLat,
        offset: new AMap.Pixel(-12.5, -30),
        icon: startIcon // 添加 Icon 图标 URL
      })
      map.add(startMarker)
      // 终点   import
      const endMarker = new AMap.Marker({
        position: endLngLat,
        offset: new AMap.Pixel(-12.5, -30),
        icon: endIcon // 添加 Icon 图标 URL
      })
      map.add(endMarker)

      // 当前点
      const currentMarker = new AMap.Marker({
        position: [
          info.value?.currentLocationInfo.longitude,
          info.value?.currentLocationInfo.latitude
        ],
        offset: new AMap.Pixel(-25, -30),
        icon: currIcon // 添加 Icon 图标 URL
      })
      map.add(currentMarker)
    })
    .catch(e => {
      console.log(e)
    })
}
onMounted(async () => {
  await getData()
  initMap()
})
</script>

<template>
  <div class="order-logistics-page">
    <div id="map">
      <div class="title">
        <van-icon name="arrow-left" @click="$router.back()" />
        <span>{{ info?.statusValue }}</span>
        <van-icon name="service" />
      </div>
      <div class="current">
        <p class="status">订单派送中 预计{{ info?.estimatedTime }}送达</p>
        <p class="predict">
          <span>{{ info?.name }}</span>
          <span>{{ info?.awbNo }}</span>
        </p>
      </div>
    </div>
    <div class="logistics">
      <p class="title">物流详情</p>
      <van-steps direction="vertical" :active="0">
        <van-step v-for="i in info?.list" :key="i.id">
          <p class="status">{{ i.statusValue }}</p>
          <p class="content">{{ i.content }}</p>
          <p class="time">{{ i.createTime }}</p>
        </van-step>
      </van-steps>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.order-logistics-page {
  --van-step-icon-size: 18px;
  --van-step-circle-size: 10px;
}
#map {
  height: 450px;
  background-color: var(--cp-bg);
  overflow: hidden;
  position: relative;
  .title {
    background-color: #fff;
    height: 46px;
    width: 355px;
    border-radius: 4px;
    display: flex;
    align-items: center;
    padding: 0 15px;
    font-size: 16px;
    position: absolute;
    left: 10px;
    top: 10px;
    box-sizing: border-box;
    box-shadow: 0px 0px 22px 0px rgba(229, 229, 229, 0.5);
    z-index: 999;
    > span {
      flex: 1;
      text-align: center;
    }
    .van-icon {
      font-size: 18px;
      color: #666;
      &:last-child {
        color: var(--cp-primary);
      }
    }
  }
  .current {
    height: 80px;
    border-radius: 4px;
    background-color: #fff;
    height: 80px;
    width: 355px;
    box-sizing: border-box;
    padding: 15px;
    position: absolute;
    left: 10px;
    bottom: 10px;
    box-shadow: 0px 0px 22px 0px rgba(229, 229, 229, 0.5);
    z-index: 999;
    .status {
      font-size: 15px;
      line-height: 26px;
    }
    .predict {
      color: var(--cp-tip);
      font-size: 13px;
      margin-top: 5px;
      > span {
        padding-right: 10px;
      }
    }
  }
}
.logistics {
  padding: 0 10px 20px;
  .title {
    font-size: 16px;
    padding: 15px 5px 5px;
  }
  .van-steps {
    :deep(.van-step) {
      &::after {
        display: none;
      }
    }
    .status {
      font-size: 15px;
      color: var(--cp-text3);
      margin-bottom: 4px;
    }
    .content {
      font-size: 13px;
      color: var(--cp-tip);
      margin-bottom: 4px;
    }
    .time {
      font-size: 13px;
      color: var(--cp-tag);
    }
  }
}
</style>
